package com.pospi.xstore.ui.caigou;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.lany.sp.SPHelper;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mylhyl.circledialog.CircleDialog;
import com.mylhyl.circledialog.params.ProgressParams;
import com.pospi.xstore.R;
import com.pospi.xstore.dto.CustomerBeen;
import com.pospi.xstore.dto.GoodsBeen;
import com.pospi.xstore.dto.MenuDto;
import com.pospi.xstore.dto.SearchBeen;
import com.pospi.xstore.dto.StoreBeen;
import com.pospi.xstore.dto.SupBeen;
import com.pospi.xstore.http.LoadServer;
import com.pospi.xstore.ui.base.BaseActivity;
import com.pospi.xstore.ui.count.CountActivity;
import com.pospi.xstore.ui.count.CountsActivity;
import com.pospi.xstore.ui.login_ui.LoginActivity;
import com.pospi.xstore.ui.login_ui.SelectStoreActivity;
import com.pospi.xstore.util.Api;
import com.pospi.xstore.util.Constant;
import com.pospi.xstore.util.GetData;
import com.pospi.xstore.util.SaveMenuInfo;
import com.pospi.xstore.util.SaveOrderInfo;
import com.zhl.cbdialog.CBDialogBuilder;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lib.kingja.switchbutton.SwitchMultiButton;
/*
* 采购订单
* */

public class CgddActivity extends BaseActivity {
    @Bind(R.id.time_start_select)
    TextView time_start_select;
    @Bind(R.id.time_end_select)
    TextView time_end_select;
    @Bind(R.id.djbh)
    EditText bill_no;
    @Bind(R.id.cgmd)
    TextView tv_shop;
    @Bind(R.id.gys)
    TextView tv_sup;
    @Bind(R.id.status_bill)
    TextView status_bill;
    String[] data = new String[]{"1号门店", "2号门店", "3号门店", "4号门店"};
    String[] data2 = new String[]{"审核", "未审核"};
    private List<CgddBeen> cgddBeens = new ArrayList<>();
    private List<StoreBeen> storeBeens;
    private List<CustomerBeen> customerBeens;
    private CustomerBeen customerBeen = new CustomerBeen();
    private SupBeen supBeen = new SupBeen();
    private String stutas = "";
    private String count;
    private String je;
    private SearchBeen searchBeen;
    private String mark;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.abtivity_cgdd);
        ButterKnife.bind(this);
        setTitle("采购单查询");
        searchBeen = new SearchBeen();
        mark = getIntent().getStringExtra("mark");
        time_start_select.setText(GetData.getYYMMDDTime());
        time_end_select.setText(GetData.getYYMMDDTime());

    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        SupBeen been = (SupBeen) intent.getSerializableExtra("sup");
        if (been != null) {
            tv_sup.setText(been.getName());
            supBeen = been;
            searchBeen.setSupid(been.getId());
        }

    }


    List<String> strings = new ArrayList<>();
    @OnClick({R.id.cgmd,R.id.gys,R.id.status_bill,R.id.bt_search,R.id.time_start_select,R.id.time_end_select})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cgmd:
                storeBeens = SaveOrderInfo.changeStoreToList(SPHelper.getInstance().getString(Constant.STORE_LIST));
                strings.clear();
                for (StoreBeen been : storeBeens) {
                    strings.add(been.getName());
                }
                creatDialog(strings);

                break;
            case R.id.gys:
                Intent intent = new Intent(this, SupActivity.class);
                intent.putExtra("mark", "CgddActivity");
                startActivity(intent);
                break;
            case R.id.status_bill:
                creatDialog2(data2);
                break;
            case R.id.bt_search:
                searchBeen.setStartRq(time_start_select.getText().toString());
                searchBeen.setEndRq(time_end_select.getText().toString());
                goBack();


//                searchBill(cgddBeen.getBillno(),supBeen.getId(),cgddBeen.getMarket(),stutas);
                break;
            case R.id.time_start_select:
                TimePickerDialog dialogYearMonthDay = new TimePickerDialog.Builder()
                        .setType(Type.YEAR_MONTH_DAY)
                        .setTitleStringId("选择日期")
                        .setWheelItemTextSize(16)
                        .setCallBack(new OnDateSetListener() {
                            @Override
                            public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                                Date date = new Date(millseconds);
                                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                                String time = format.format(date);
                                time_start_select.setText(time);
                            }
                        })
                        .build();
                dialogYearMonthDay.show(getSupportFragmentManager(), "YEAR_MONTH_DAY");
                break;
            case R.id.time_end_select:
                TimePickerDialog dialogYearMonthDay2 = new TimePickerDialog.Builder()
                        .setType(Type.YEAR_MONTH_DAY)
                        .setTitleStringId("选择日期")
                        .setWheelItemTextSize(16)
                        .setCallBack(new OnDateSetListener() {
                            @Override
                            public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                                Date date = new Date(millseconds);
                                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                                String time = format.format(date);
                                time_end_select.setText(time);
                            }
                        })
                        .build();
                dialogYearMonthDay2.show(getSupportFragmentManager(), "YEAR_MONTH_DAY");
                break;
//            case R.id.bt_threeday:
//
//                break;
        }
    }

    private void goBack() {
        switch (mark) {
            case "cgdd":
                Intent intentback = new Intent(CgddActivity.this,CgshListActivity.class);
                intentback.putExtra("data",searchBeen);
                startActivity(intentback);
                break;
            case "cgsh":
                Intent cgsh = new Intent(CgddActivity.this,ReceiveBillListActivity.class);
                cgsh.putExtra("data",searchBeen);
                startActivity(cgsh);
                break;
            case "cgth":
                Intent cgth = new Intent(CgddActivity.this,RefundBillListActivity.class);
                cgth.putExtra("data",searchBeen);
                startActivity(cgth);
                break;
        }
    }

    private CgddBeen cgddBeen = new CgddBeen();
    private void creatDialog(List<String> ss) {

        new CircleDialog.Builder(this)
                .setItems(ss, new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        //门店
                        tv_shop.setText(storeBeens.get(i).getName());

                        cgddBeen.setMarket(storeBeens.get(i).getId());
                        searchBeen.setMarket(storeBeens.get(i).getId());
                    }
                })
                .setGravity(Gravity.CENTER)
                .show();
    }
    private void creatDialog2(final String[] ss) {

        new CircleDialog.Builder(this)
                .setItems(ss, new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        //
                        status_bill.setText(ss[i]);
                        switch (ss[i]) {
                            case "审核":
                                stutas = "2";
                                break;
                            case "未审核":
                                stutas = "1";
                                break;
                        }
                        searchBeen.setStatus(stutas);
                    }
                })
                .setGravity(Gravity.CENTER)
                .show();
    }
}
